﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kortex.WallMgr;
using System.Net;
using System.Windows.Forms;
using Barco.Web.KPHttpServer;

namespace Kortex.WallManagerControl.REST_Server
{
    public class RestServer
    {
        WallManager Wall = null;
        public MyWebServer HttpServer = null;

        public RestServer(WallManager wObj)
        {
            Wall = wObj;
        
        }


        public void PostRequest( HttpListenerContext Contex, string Json)
        {
            //MessageBox.Show(Json);
            
            
            if (Json.Contains("idle"))
            {
                string json = "{ \"resource\" : { \"name\" : \"http://123.333.222.222/dramp/2/wall/1,1/actions/8476\"}, \"action\" : { \"name\" : \"idle\",\"state\" : \"STATE_IN_PROGRESS\",\"seq\" : 52,\"value\" : 8476 }}";
                Wall.SetWallidle();
                //HttpServer.RespondToRequester(Contex, "Well i am going to Switch off Wall On Post Request");
                HttpServer.RespondToRequester(Contex, json);
            }
            else if (Json.Contains("on"))
            {
                string json = "{ \"resource\" : { \"name\" : \"http://123.333.222.222/dramp/2/wall/1,1/actions/8476\"}, \"action\" : { \"name\" : \"on\",\"state\" : \"STATE_IN_PROGRESS\",\"seq\" : 52,\"value\" : 8476 }}";
                Wall.SetWallOn();
                //HttpServer.RespondToRequester(Contex, "Well i am going to Switch on Wall On Post Request");
                HttpServer.RespondToRequester(Contex, json);
            }
            else
            {
                HttpServer.RespondToRequester(Contex, "Well this command is not Supported");
            }
            //HttpServer.RespondToRequester(Contex, "Well i am goign to do a POST");
            
        }

        public void GetRequest(HttpListenerContext Contex, string Json)
        {
            //MessageBox.Show(Json);

            if (Json.Contains("idle"))
            {
                string json = "{ \"resource\" : { \"name\" : \"http://123.333.222.222/dramp/2/wall/1,1/actions/8476\"}, \"action\" : { \"name\" : \"idle\",\"state\" : \"STATE_IN_PROGRESS\",\"seq\" : 52,\"value\" : 8476 }}";
                Wall.SetWallidle();
                HttpServer.RespondToRequester(Contex, json);
            }

            else if(Json.Contains("on"))
            {
                string json = "{ \"resource\" : { \"name\" : \"http://123.333.222.222/dramp/2/wall/1,1/actions/8476\"}, \"action\" : { \"name\" : \"on\",\"state\" : \"STATE_IN_PROGRESS\",\"seq\" : 52,\"value\" : 8476 }}";
                Wall.SetWallOn();
                HttpServer.RespondToRequester(Contex, json);
            }
            else
                HttpServer.RespondToRequester(Contex, "Well this command is not Supported");

        }

        public void StartServer(int port, string tempPath)
        {
            HttpServer.StartHttp(port, "");
            
        
        }

        public bool isRestRunning
        {
            get
            {
                return HttpServer.isRunning;
            }
        
        }

        public void StopRestServer()
        {
            HttpServer.StopServer();
        
        }
        

    }
}
